package top.itpath.common;

import com.auth0.jwt.exceptions.JWTVerificationException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.itpath.common.exp.NoAuthException;

/**
 * 全局错误处理
 *
 * @author zhaomengru
 * @time 2023/10/10 - 22:59
 */
@Slf4j
@RestControllerAdvice
public class AdviceController {

    @ExceptionHandler(NoAuthException.class)
    Result<?> handleNoAuthException(NoAuthException e) {
        log.error(e.getMessage(), e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(PersistenceException.class)
    Result<?> handleIbatisException(PersistenceException e) {
        log.error(e.getMessage(), e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    Result<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(JWTVerificationException.class)
    Result<?> handleJWTVerificationException(JWTVerificationException e) {
        log.error(e.getMessage(), e);
        return Result.fail(401, e.getMessage());
    }
}
